package com.itcast.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itcast.constant.MessageConstant;
import com.itcast.entity.Result;
import com.itcast.pojo.OrderSetting;
import com.itcast.service.OrdersettingService;
import com.itcast.untils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrdersettingController {
    @Reference
    private OrdersettingService ordersettingService;

    /**
     * 上传文件
     * @param multipartFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile")MultipartFile multipartFile){
    //调用文件上传工具类
        try {
            //读文件
            List<String[]> readExcel = POIUtils.readExcel(multipartFile);
            ordersettingService.add(readExcel);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    /**
     * 查询预约信息
     * @param date
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll(@RequestParam("date")String date){
        try {
            List<Map> list = ordersettingService.findAll(date);
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }
    @RequestMapping("/update")
    /**
     * 修改可预约的人数
     * @param orderSetting
     * @return
     */
    public Result update(@RequestBody OrderSetting orderSetting){
        try {
            ordersettingService.update(orderSetting);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
